
{% extends 'base_main.html' %}

{% block title %}
    天天生鲜-购物车
{% endblock %}

{% block content %}
	<div class="total_count">全部商品<em>2</em>件</div>
	<ul class="cart_list_th clearfix">
		<li class="col01">商品名称</li>
		<li class="col02">商品单位</li>
		<li class="col03">商品价格</li>
		<li class="col04">数量</li>
		<li class="col05">小计</li>
		<li class="col06">操作</li>
	</ul>
    {% for goods in result %}
	<ul class="cart_list_td clearfix" id="goods_{{ goods.0.id }}">
		<li class="col01"><input id="ddf_{{ goods.0.id }}" type="checkbox" value="{{ goods.2 }}" {% if goods.2 %} checked {% endif %} onclick="status({{ goods.0.id }});"></li>
		<li class="col02"><img src="/media/{{ goods.0.goods_front_image }}"></li>
		<li class="col03">{{ goods.0.name }}<br><em>{{ goods.0.shop_price }}元/500g</em></li>
		<li class="col04">500g</li>
		<li class="col05">{{ goods.0.shop_price }}元</li>
		<li class="col06">
			<div class="num_add">
				<a href="javascript:;" class="add fl"
                   onclick="add_cart({{ goods.0.id }});">
                    +
                </a>
				<input type="text" class="num_show fl" value="{{ goods.1 }}" id="num_show_{{ goods.0.id }}">
				<a href="javascript:;" class="minus fl"
                   onclick="sub_cart({{ goods.0.id }});">
                    -
                </a>
			</div>
		</li>
		<li class="col07">{{ goods.3 }}元</li>
		<li class="col08"><a href="javascript:;" onclick="del_cart({{ goods.0.id }});">删除</a></li>
	</ul>
    {% endfor %}


	<ul class="settlements">
		<li class="col01"><input type="checkbox" onclick="alert('嘻嘻，暂不不提供')" checked="" ></li>
		<li class="col02">全选</li>
		<li class="col03">合计(不含运费)：<span>¥</span><em>0</em><br>共计<b>0</b>件商品</li>
		<li class="col04"><a href="{% url 'order:place_order' %}">去结算</a></li>
	</ul>
    {% csrf_token %}
    <script>
        <!--刷新价格/件数-->
        function cart_price(){
            $.get('/cart/cart_price/', function(data){
                if(data.code == '200'){
                    $('.total_count em').html(data.all_total)
                    $('.settlements .col03 em').html(data.all_price)
                    $('.settlements .col03 b').html(data.is_select_num)
                }
            })
        }
        cart_price()

        <!--添加商品数量-->
        function add_cart(id){
            var num = $('#num_show_' + id).val()
            var new_num = parseInt(num) + 1
            $('#num_show_'+id).val(new_num)
            change_cart(id, new_num)
        }
        <!--减少商品数量-->
        function sub_cart(id){
            var num = $('#num_show_' + id).val()
            if(parseInt(num)>1){
                var new_num = parseInt(num) - 1
                $('#num_show_'+id).val(new_num)
                change_cart(id, new_num)
            }
        }
        <!--删除商品-->
        function del_cart(id){
            alert('是否删除，很危险的哦')
            var csrf = $('input[name="csrfmiddlewaretoken"]').val()
            $.ajax({
                url:'/cart/del_cart/'+id+'/',
                type:'POST',
                <!--data:{'goods_id':id},-->
                headers:{'X-CSRFToken': csrf},
                dataType:'json',
                success:function(data){
                    <!--刷新整个页面-->
                    <!--location.href='/cart/cart/'-->
                    <!--location.reload()-->
                    <!--移除ul元素,局部刷新-->
                    $('#goods_'+id).remove()
                    cart_price()
                },
                error:function(data){
                    alert('失败')
                }
            })
        }

        <!--修改状态-->
        function status(id){
            var cyy1 = $('#ddf_'+id).val()
            var num = $('#num_show_' + id).val()
            if (cyy1 == 'True'){
                console.log(cyy1)
                var dyf = '0'
                alert('1->0')
                change_cart(id,num,dyf)
                return 0
            }
            if (cyy1 == 'False'){
                console.log(cyy1)
                var dyf = '1'
                alert('0->1')
                change_cart(id,num,dyf)
                return 0
            }
        }
        <!--修改数量或选择状态-->
        function change_cart(id, new_num, dyf){
            var csrf = $('input[name="csrfmiddlewaretoken"]').val()
            $.ajax({
                url:'/cart/change_cart/',
                type:'POST',
                data:{'goods_id':id, 'goods_num':new_num, 'goods_select':dyf},
                headers:{'X-CSRFToken': csrf},
                dataType:'json',
                success:function(data){
                    <!--刷新数量/总价/数量-->
                    location.reload()
                    cart_price()
                },
                error:function(data){
                }
            })
        }
    </script>
{% endblock %}

